package project.android;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ManageGroups extends ListActivity{

	static ArrayList<String> LIST ;
	static String guid[];
	Context context;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        
        String[] received = null;
        context = this;
        
        try{
        	received = Synchronized.syncGroups(new DatabaseConnection(this),null,"groups/getgroups");
        }
        catch(Exception e){
        	new AlertDialog.Builder(this)
        	.setTitle("Server Connection Error")
        	.setMessage(e.getStackTrace().toString())
	        .setNeutralButton(android.R.string.cancel,
	            new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		        	   finish();
		           }
	        })
	        .show();
        }
        
        
        //create LIST array with group names
        LIST = new ArrayList<String>();
        if(received == null || received.length == 0){
        	
        	LIST.add("You are not a member of any groups yet!");
        	
        }
        else{
        	
        	guid = new String[received.length-1];
        	for(int i = 0;i<received.length-1;i++){
        		String[] n = received[i].split("~");
        		guid[i] = new String();
        		LIST.add(n[1]);
        		guid[i] = n[0];	
        		
        	}
        	
        }
      	
        final ListView lv = getListView(); 
        lv.setTextFilterEnabled(true);
        Button addUserButton = new Button(this);
        addUserButton.setText("Add New Group");
        addUserButton.setOnClickListener(new View.OnClickListener() {
        	
            public void onClick(View v) {
            	
            	LayoutInflater factory = LayoutInflater.from(ManageGroups.this);
    	        final View input = factory.inflate(R.layout.new_group, null);
    	        new AlertDialog.Builder(ManageGroups.this)
    	        	.setView(input)
    	        	.setPositiveButton("Save", 
    	        		new DialogInterface.OnClickListener() {
    			        	public void onClick(DialogInterface dialog, int whichButton){
    			        		
    			        		String toBeSend =
    			        				((EditText) input.findViewById(R.id.groupNameIn1)).getText().toString();
    				         
    			        		try{
    			                	Synchronized.syncGroups(new DatabaseConnection(context),toBeSend,"groups/addgroup");
    			                }
    			                catch(Exception e){}
    			                
    			        	} 	
    	        	})
    		        .setNeutralButton(android.R.string.cancel,
    		            new DialogInterface.OnClickListener() {
    			           public void onClick(DialogInterface dialog, int whichButton){
    			        	   
    			        	   finish();
    			           
    			           }
    		        })
    		        .show();
            	
            }
        });
        
        lv.addFooterView(addUserButton);
        lv.setTextFilterEnabled(true);
        GroupListViewAdapter adapter = new GroupListViewAdapter(this, LIST, guid);
        setListAdapter(adapter);        
        
	}
	
}
